#pragma once

#include "serverApdu.hpp"

class GetResponseNormalApdu : public ServerApdu {
public:
    GetResponseNormalApdu(const ByteStream &b)
        : ServerApdu(b){};

    virtual DataValueType value(void) const override {
        if (_stream[8] == Dlt698DataType::DATA_TYPE_LONG_UNSIGNED || _stream[8] == Dlt698DataType::DATA_TYPE_LONG) {
            return {std::make_pair(_stream[8], ByteStream(_stream.begin() + 9, _stream.begin() + 11))};
        } else if (_stream[8] == Dlt698DataType::DATA_TYPE_ARRAY) {
            auto arraySize = _stream[9];
            auto itemType = _stream[10];
            auto itemSize = 0;
            DataValueType v{};

            if (itemType == Dlt698DataType::DATA_TYPE_LONG_UNSIGNED) {
                itemSize = 2;
            }

            for (auto i = 0; i < arraySize; i++) {
                auto begin = _stream.begin() + 11 + (i * (1 + itemSize));
                auto end = begin + itemSize;
                v.emplace_back(std::make_pair(_stream[10 + i * (1 + itemSize)], ByteStream(begin, end)));
            }
            return v;
        } else if (_stream[8] == Dlt698DataType::DATA_TYPE_DOUBLE_LONG || _stream[8] == Dlt698DataType::DATA_TYPE_DOUBLE_LONG_UNSIGNED) {
            return {std::make_pair(_stream[8], ByteStream(_stream.begin() + 9, _stream.begin() + 13))};
        }

        return {};
    }
};
